﻿namespace CgSilverlightDynamicGeometry
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Collections.Generic;
    using System.Linq;
    using CgSilverlightDynamicGeometry.Utilities;

    public class CartesianLines
    {
        List<Line> Lines = new List<Line>();

        private Drawing drawing;
        public Drawing Drawing
        {
            get { return drawing; }
            set
            {
                if (Drawing == value)
                {
                    return;
                }
                drawing = value;
            }
        }

        /// <summary>
        /// 更新显示
        /// </summary>
        public void UpdateVisual()
        {
            CoordinateSystem coordinateSystem = Drawing.CoordinateSystem;
            var xPoints = coordinateSystem.GetVisibleXPoints();
            var yPoints = coordinateSystem.GetVisibleYPoints();

            int count = xPoints.Count() + yPoints.Count();
            if (Lines.Count<count)
            {
                AddNewLine(count - Lines.Count);
            }
            else if (Lines.Count>count)
            {
                RemoveExcessLines(Lines.Count - count);
            }

            int i = 0;
            foreach (var x in xPoints)
            {
                MoveLineX(Lines[i++], x, coordinateSystem);
            }
            foreach (var y in yPoints)
            {
                MoveLineY(Lines[i++], y, coordinateSystem);
            }
        }

        /// <summary>
        /// 添加坐标线
        /// </summary>
        /// <param name="count">数量</param>
        private void AddNewLine(int count)
        {
            for (int i = 0; i < count; i++)
            {
                var newLine = new Line();
                newLine.SetZIndex((int)ZOrder.Grid);
                Lines.Add(newLine);
                Drawing.Canvas.Children.Add(newLine);
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="count">数量</param>
        private void RemoveExcessLines(int count)
        {
            for (int i = Lines.Count-count; i < Lines.Count; i++)
            {
                Drawing.Canvas.Children.Remove(Lines[i]);
            }
            Lines.RemoveRange(Lines.Count - count, count);
        }

        /// <summary>
        /// 绘制X方向线条
        /// </summary>
        /// <param name="line">绘制的线</param>
        /// <param name="x">X方向移动量</param>
        /// <param name="coordinateSystem"></param>
        private void MoveLineX(Line line, double x, CoordinateSystem coordinateSystem)
        {
            if (x==0)
            {
                line.Stroke = new SolidColorBrush(Colors.Green);
                line.StrokeThickness = 1.5;
            }
            else
            {
                line.Stroke = new SolidColorBrush(Colors.Gray);
                line.StrokeThickness = .5;
            }

            line.Move(
                x,
                coordinateSystem.MinimalVisibleY,
                x,
                coordinateSystem.MaximalVisibleY,
                coordinateSystem);
        }

        /// <summary>
        /// 绘制Y方向线条
        /// </summary>
        /// <param name="line">绘制的线</param>
        /// <param name="y">Y方向移动量</param>
        /// <param name="coordinateSystem"></param>
        private void MoveLineY(Line line, double y, CoordinateSystem coordinateSystem)
        {
            if (y==0)
            {
                line.Stroke = new SolidColorBrush(Colors.Green);
                line.StrokeThickness = 1.5;
            }
            else
            {
                line.Stroke = new SolidColorBrush(Colors.Gray);
                line.StrokeThickness = .5;
            }

            line.Move(
                coordinateSystem.MinimalVisibleX,
                y,
                coordinateSystem.MaximalVisibleX,
                y,
                coordinateSystem);
        }
    }
}
